/*
 * Created on 2005-12-14
 */
package cn.com.pxto.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;


import cn.com.pxto.global.varables.*;
import cn.com.pxto.model.User;
import cn.com.pxto.service.RuleManager;
import cn.com.pxto.service.UserManager;
import cn.com.pxto.web.listener.LogedSessionListener;


/**
 * 
 * @author Gauss Cheung
 * 
 */
public class ComUserLogonAction extends Action {
	private Log log  = LogFactory.getLog(ComUserLogonAction.class);
	private UserManager userManager = null;
	private RuleManager ruleManager = null;
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	public void setRuleManager(RuleManager ruleManager) {
		this.ruleManager = ruleManager;
	}


	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm logonForm = (DynaActionForm) form;
		String userName = (String) logonForm.get("userName");
		String password = (String) logonForm.get("password");

		ActionMessages errors = new ActionMessages();
		User user = userManager.getUser(userName);
		if (user != null) {
			Integer type = user.getType();		
//			sysorg:2 user:3
			if (type.intValue()==2 || type.intValue()==3) {
				request.getSession().setAttribute("type", type);//类型放入session
				//增加用户id
				request.getSession().setAttribute("uid", user.getUid());
				if (user.getPassword().equals(password)){
					List functions = ruleManager.getUserFunctions(user.getUid());
					LogonInfo logonInfo = new LogonInfo(userName,password,functions,user.getUid());
					request.getSession().setMaxInactiveInterval(1800);
					request.getSession().setAttribute("logonInfo", logonInfo);
					request.getSession().setAttribute("LogedSessionListener",new LogedSessionListener(request.getSession().getServletContext()));
				} else {
					ActionMessage error = new ActionMessage(
							"error.logon.InvalidPassword");
					errors.add(ActionMessages.GLOBAL_MESSAGE, error);
					saveErrors(request, errors);
					request.setAttribute("errForMsg","密码错误！");
					request.setAttribute("userName",userName);
					return mapping.findForward("return");
				}
			} else {
				ActionMessage error = new ActionMessage(
				"error.invalid_login_mode");
				errors.add(ActionMessages.GLOBAL_MESSAGE, error);
				saveErrors(request, errors);
				request.setAttribute("errorMsg", "错误的用户登录类型");
				return mapping.findForward("return");
			}
		}else {//�޴��û�
			ActionMessage error = new ActionMessage("error.logon.noSuchUser");
			errors.add(ActionMessages.GLOBAL_MESSAGE, error);
			saveErrors(request, errors);
			request.setAttribute("msg", "�޴��û�!");
			return mapping.findForward("return");
		}
		return mapping.findForward("comUserLoged");
	}
}
